package com.guigu.erp.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.DesignFileCountDto;
import com.guigu.erp.pojo.DesignConfigFileKind;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.service.DesignConfigFileKindService;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/design/file")
public class DesignFileController {
    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private DesignConfigFileKindService designConfigFileKindService;

    @Autowired
    private NumberUtils numberUtils;

    // 分类统计：按厂商统计产口档案数量
    @RequestMapping("/countByFactoryName")
    @ResponseBody
    public List<DesignFileCountDto> countByFactoryName() {
        return designFileService.countByFactoryName();
    }


    //产品设计 -- 产品档案查询的请求
    @RequestMapping("/list")
    @ResponseBody
    public PageUtils<DesignFile> list(DesignFileVo vo,int pageNo,int pageSize){
        PageUtils<DesignFile> page = designFileService.page(vo, pageNo, pageSize);
        return page;
    }

    //产品设计 -- 产品查询下拉框条件的请求
    @RequestMapping("/select")
    @ResponseBody
    public List<DesignConfigFileKind> select(String kid){
        QueryWrapper<DesignConfigFileKind> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(kid)){
            wrapper.eq("kind_level",kid);
        }
        List<DesignConfigFileKind> kindList = designConfigFileKindService.list(wrapper);

        return kindList;
    }

    //产品设计 -- 产品档案登记
    @RequestMapping("/add")
    @ResponseBody
    public boolean add(DesignFile designFile){
        //重新设置各级编号和名称
        String firstKindName=designFile.getFirstKindName();
        String[] firstAry=firstKindName.split("/");
        designFile.setFirstKindId(firstAry[1]);
        designFile.setFirstKindName(firstAry[2]);

        String secondKindName=designFile.getSecondKindName();
        String[] secondAry=secondKindName.split("/");
        designFile.setSecondKindId(secondAry[1]);
        designFile.setSecondKindName(secondAry[2]);

        String thirdKindName=designFile.getThirdKindName();
        String[] thirdAry=thirdKindName.split("/");
        designFile.setThirdKindId(thirdAry[1]);
        designFile.setThirdKindName(thirdAry[2]);


        //设置标志位属性
        //审核标志 CHECK_TAG : 等待审核
        designFile.setCheckTag("S001-0");
        //档案变更标志 CHANGE_TAG : 未变更
        designFile.setChangeTag("D002-0");
        //价格变更标志 PRICE_CHANGE_TAG：未变更
        designFile.setPriceChangeTag("J001-0");
        //产品删除标志 DELETE_TAG: 未删除
        designFile.setDeleteTag("C001-0");
        //物料组成标志 DESIGN_MODULE_TAG: 未设计
        designFile.setDesignModuleTag("W001-0");
        //工序组成标志 DESIGN_PROCEDURE_TAG: 未设计
        designFile.setDesignProcedureTag("G001-0");
        //库存分配标志 DESIGN_CELL_TAG: 未设计
        designFile.setDesignCellTag("K001-0");

        //生产对应的生产编号
        String number=numberUtils.getProductNumber(designFile.getFirstKindId(),designFile.getSecondKindId(),designFile.getThirdKindId());

        //设置产品编号
        designFile.setProductId(number);

        //插入数据
        boolean flag = designFileService.save(designFile);
        return flag;
    }


    //产品设计  -- 产品档案登记里面下拉框的请求
    @RequestMapping("kind")
    @ResponseBody
    public List<DesignConfigFileKind> kind(int kid,String kindLevel){
        List<DesignConfigFileKind> fileKinds = designConfigFileKindService.getKindId(kid, kindLevel);
        return fileKinds;
    }

    //产品设计  -- 产品档案登记复核查询
    @RequestMapping("review")
    @ResponseBody
    public List<DesignFile> review(){
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<DesignFile>();
        queryWrapper.eq("CHECK_TAG","S001-0");
        queryWrapper.eq("DELETE_TAG","C001-0");
        List<DesignFile> fileList = designFileService.list(queryWrapper);
        return fileList;
    }

    //产品设计  -- 产品档案登记复核数量
    @RequestMapping("reviewCount")
    @ResponseBody
    public int reviewCount(){
        return designFileService.getCount();
    }

   //产品设计  -- 产品档案复核根据id查询复核信息
    @RequestMapping("getId")
    @ResponseBody
    public DesignFile designFileGetId(Integer id){
        DesignFile designFile = designFileService.getById(id);
        return designFile;
    }

    //产品设计 --产品复核通过
    @RequestMapping("reviewPassed")
    @ResponseBody
    public boolean reviewPassed(DesignFile designFile){
        //重新设置各级编号和名称
        String firstKindName=designFile.getFirstKindName();
        String[] firstAry=firstKindName.split("/");
        designFile.setFirstKindId(firstAry[0]);
        designFile.setFirstKindName(firstAry[1]);

        String secondKindName=designFile.getSecondKindName();
        String[] secondAry=secondKindName.split("/");
        designFile.setSecondKindId(secondAry[0]);
        designFile.setSecondKindName(secondAry[1]);

        String thirdKindName=designFile.getThirdKindName();
        String[] thirdAry=thirdKindName.split("/");
        designFile.setThirdKindId(thirdAry[0]);
        designFile.setThirdKindName(thirdAry[1]);

        designFile.setCheckTag("S001-1");
        boolean flag = designFileService.updateById(designFile);
        return flag;
    }


    //产品设计 -- 未制定物料组成设计单的查询数据(带条件)
    @RequestMapping("unMakeMaterialList")
    @ResponseBody
    public PageUtils<DesignFile> unMakeMaterialList(DesignFileVo vo,Integer pageNo,Integer pageSize){
        PageUtils<DesignFile> pageUtils = designFileService.getUnMakeMaterial(vo, pageNo, pageSize);
        return pageUtils;
    }

    //产品设计 -- 查询产品档案的物料数据
    @RequestMapping("makeMaterialList")
    @ResponseBody
    public PageUtils<DesignFile> makeMaterialList(DesignFileVo vo,Integer pageNo,Integer pageSize){
        PageUtils<DesignFile> makematerialList = designFileService.getMakematerialList(vo, pageNo, pageSize);
        return makematerialList;
    }

    //产品设计 -- 制定物料设计单 -- 点击制定设计单
    @RequestMapping("getMaterialOne")
    @ResponseBody
    public DesignFile getMaterialOne(int id){
        DesignFile designFile = designFileService.getById(id);
        return designFile;
    }

    //产品设计 -- 制定物料设计单 -- 点击制定设计单
    @RequestMapping("getProductId")
    @ResponseBody
    public DesignFile getProductId(String pid){
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",pid);
        DesignFile designFile = designFileService.getOne(wrapper);
        return designFile;
    }

    //生产管理  -- 查询未制定工序组成标志
    @RequestMapping("/queryUnMakeProcessList")
    @ResponseBody
    public PageUtils<DesignFile> queryUnMakeProcessList(DesignFileVo vo,Integer pageNo,Integer pageSize){
        PageUtils<DesignFile> makeMaterialList = designFileService.getUnMakeProcessList(vo, pageNo, pageSize);
        return makeMaterialList;
    }

    //查询未制定库存管理
    @RequestMapping("/unMakeSecurityConfiguration")
    @ResponseBody
    public PageUtils<DesignFile> unMakeSecurityConfiguration( DesignFileVo vo, Integer pageNo,  Integer pageSize){
        PageUtils<DesignFile> pageUtils = designFileService.getUnMakeSecurityConfiguration(vo, pageNo, pageSize);
        return pageUtils;
    }

    @RequestMapping("/unMakeSecurityConfiguration1")
    @ResponseBody
    public PageUtils<DesignFile> unMakeSecurityConfiguration1( DesignFileVo vo,  Integer pageNo, Integer pageSize){
        PageUtils<DesignFile> pageUtils = designFileService.getUnMakeSecurityConfiguration(vo, pageNo, pageSize);
        return pageUtils;
    }

    //入库申请查询已审核通过的产品档案
    @RequestMapping("queryByIdInStorage")
    @ResponseBody
    public PageUtils<DesignFile> queryByIdInStorage(DesignFileVo vo,Integer pageNo,Integer pageSize){
        PageUtils<DesignFile> pageUtils = designFileService.getSecurityConfiguration(vo, pageNo, pageSize);
        return pageUtils;
    }

    //生产计划单的要添加的产品档案
    @RequestMapping("queryProductsIsCommodity")
    @ResponseBody
    public PageUtils<DesignFile> queryProductsIsCommodity(DesignFileVo vo,Integer pageNo,Integer pageSize){
        PageUtils<DesignFile> commodity = designFileService.getProductsIsCommodity(vo, pageNo, pageSize);
        return commodity;
    }
}
